package com.example.learn_spring_boot3.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.learn_spring_boot3.entity.User;
import com.example.learn_spring_boot3.entity.UserOrder;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserOrderMapper extends BaseMapper<UserOrder> {

    @Select("SELECT * FROM learn_spring_boot3.user_order WHERE user_id=#{uid}")
    List<UserOrder> selectByUid(int uid);


    @Select("SELECT * FROM learn_spring_boot3.user_order")
    // 这里定义结果集映射
    @Results({
            // 字段一一对应
            @Result(column = "id", property = "id"),
            @Result(column = "user_id", property = "userId"),
            @Result(column = "name", property = "name"),

            @Result(
                    // 使用user_id去查询, 对应实体类的user字段, 返回值类型为 User
                    column = "user_id", property = "user", javaType = User.class,
                    // 一对一查询, select表示引用其它mapper的方法, 会把 user_id 的值传给他
                    one = @One(select = "com.example.learn_spring_boot3.mapper.UserMapper.selectById")
            ),
    })
    List<UserOrder> selectAllOrdersAndUser();
}
